package com.yadang.csv;

import com.opencsv.bean.StatefulBeanToCsv;
import com.opencsv.bean.StatefulBeanToCsvBuilder;
import com.opencsv.exceptions.CsvDataTypeMismatchException;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;

import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * @author: 亚当
 * @date: 2023年05月16日 21:51
 * @package: com.yadang.csv
 * @Description: TODO
 */
public class TestCsv {
    public static void main(String[] args) {
        User user = new User();
        user.setUserName("张三");
        user.setSex("男");
        user.setAge(18);
        List list = new LinkedList<User>();
        list.add(user);
        //exportDataToCSV();

    }
    public static void exportDataToCSV(PrintWriter writer) {
        try {
            User user = new User();
            user.setUserName("张三");
            user.setSex("男");
            user.setAge(18);
            List list = new LinkedList<User>();
            list.add(user);
            StatefulBeanToCsv<User> beanToCsv = new StatefulBeanToCsvBuilder<User>(writer).build();
            beanToCsv.write(list);

        } catch (CsvDataTypeMismatchException | CsvRequiredFieldEmptyException e) {
            // Handle exceptions
            e.printStackTrace();
        }
    }
}
